/**
 * 
 */
package cn.ad.server.web.servlet.v2.parser;


import cn.ad.server.common.utils.BrowserUtils;
import cn.ad.server.web.spring.model.AdRequest;
import cn.ad.server.web.spring.model.sts.STSAdRequest;
import cn.ad.server.web.spring.service.actions.context.AdContext;
import cn.ad.server.web.spring.service.actions.context.AdContextBuilder;
import cn.ad.server.web.utils.HttpUtils;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * Server2Server 协议请求解析类
 * @author Wu Chengjun
 *
 */
public class STSAdRequestParser extends AbstractHttpServletJsonParser {
	
	/* (non-Javadoc)
	 * @see cn.ad.server.web.servlet.parser.AbstractHttpServletJsonParser#parse(javax.servlet.http.HttpServletRequest)
	 */
	@Override
	public AdRequest parse(HttpServletRequest request) throws AdRequestParseException {
		STSAdRequest req = (STSAdRequest) super.parse(request);
		AdContext stsContext = AdContextBuilder.build(req);
		//获取渠道标识
		String ch = HttpUtils.getPathParam(request.getServletPath(), "ch");
		if(StringUtils.isEmpty(ch) || StringUtils.isBlank(ch)){
			throw new AdRequestParseException("传入非法参数，ch is null");
		}
		req.setChannelID(ch);
		// 浏览器解析
		String ua = request.getHeader("user-agent");
		String browser = BrowserUtils.getBrowser(ua);
		stsContext.setBrowser(browser);
		stsContext.setUa(ua);
		stsContext.setReferer(request.getHeader("Referer"));
		req.setAdContext(stsContext);
		return req;
	}
	

	
	

	/* (non-Javadoc)
	 * @see cn.ad.server.web.servlet.parser.AbstractHttpServletJsonParser#getAdRequestClassType()
	 */
	@Override
	public Class getAdRequestClassType() {
		return STSAdRequest.class;
	}
}
